package com.hinnotek.amoeba.widget;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hinnotek.amoeba.R;


/**
 * Created by Frank on 2018/7/19.
 */

public class MyTitleBar extends LinearLayout implements View.OnClickListener{

    private TextView mLeftTv, mCenterTv, mCenterTipTv, mRightTv;
    private LinearLayout mLeftLayout;
    private RelativeLayout mTitleBarLayout,mRightLayout;
    private ImageView mLeftImg, mRightImg;
    private ITitleBarListener mTitleBarListener;
    private LinearLayout addLayout;


    public MyTitleBar(Context context) {
        super(context);
        init(context);
    }

    public MyTitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public MyTitleBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View titleView = inflater.inflate(R.layout.my_title_bar, this);
        mTitleBarLayout = (RelativeLayout) titleView.findViewById(R.id.actionbar_layout);

        // 左边
        mLeftLayout = (LinearLayout) titleView.findViewById(R.id.actionbar_left_layout);
        mLeftImg = (ImageView) titleView.findViewById(R.id.actionbar_left_img);
        mLeftTv = (TextView) titleView.findViewById(R.id.actionbar_left_tv);

        // 中间
        mCenterTv = (TextView) titleView.findViewById(R.id.actionbar_center_tv);
        mCenterTipTv = (TextView) titleView.findViewById(R.id.actionbar_center_tip_tv);
        addLayout = titleView.findViewById(R.id.addLayout);

        // 右边
        mRightLayout = (RelativeLayout) titleView.findViewById(R.id.actionbar_right_layout);
        mRightImg = (ImageView) titleView.findViewById(R.id.actionbar_right_img);
        mRightTv = (TextView) titleView.findViewById(R.id.actionbar_right_tv);

        // 初始化事件
        initEvents();
    }

    // 初始化事件
    private void initEvents() {
        mLeftLayout.setOnClickListener(this);
        mRightLayout.setOnClickListener(this);
    }



    /*设置标题栏背景色*/
    public void setBarBackgroundColor(int color){
        mTitleBarLayout.setBackgroundColor(color);
    }


    /**
     * 设置TitleBar事件
     */
    public void setTitleBarListener(ITitleBarListener mTitleBarListener) {
        this.mTitleBarListener = mTitleBarListener;
    }

    /**
     * 设置左边文本
     */
    public void setLeftText(String value) {
        if (TextUtils.isEmpty(value)) {
            mLeftTv.setVisibility(View.GONE);
        } else {
            mLeftTv.setVisibility(View.VISIBLE);
            mLeftTv.setText(value);
        }
    }

    /**
     * 设置左边图片
     */
    public void setLeftImage(int resId) {
        mLeftImg.setImageResource(resId);
    }

    /**
     * 设置左边是否可见
     */
    public void setLeftVisible(boolean isVisibled) {
        if (isVisibled) {
            mLeftLayout.setVisibility(View.VISIBLE);
        } else {
            mLeftLayout.setVisibility(View.GONE);
        }
    }

    /**
     * 设置标题（主）
     */
    public void setTitleValue(String value) {


        if (mCenterTv.getVisibility() == View.GONE){
            mCenterTv.setVisibility(View.VISIBLE);
            if (value == null)
                value = "";
            mCenterTv.setText(value);
        }else{
            if (value == null)
                value = "";
            mCenterTv.setText(value);
        }


    }

    /**
     * 设置标题（副）
     */
    public void setTitleTipValue(String value) {
        if (value == null)
            value = "";
        mCenterTipTv.setText(value);
        mCenterTipTv.setVisibility(View.VISIBLE);
    }



    public void setAddLayout(View view){
        if (addLayout.getVisibility() == View.GONE){
            addLayout.setVisibility(View.VISIBLE);
            addLayout.addView(view);
        }else{
            addLayout.addView(view);
        }
    }




    /**
     * 设置右边文本
     */
    public void setRightText(String value) {
        if (value == null) {
            mRightTv.setVisibility(View.GONE);
        } else {
            mRightTv.setVisibility(View.VISIBLE);
            mRightTv.setText(value);
            setRightVisible(true);
        }
    }

    /**
     * 设置右边图片
     */
    public void setRightImage(int resId) {
        mRightImg.setImageResource(resId);
        mRightImg.setVisibility(View.VISIBLE);
        setRightVisible(true);
    }

    /**
     * 设置右边是否可见
     */
    public void setRightVisible(boolean isVisibled) {
        if (isVisibled) {
            mRightLayout.setVisibility(View.VISIBLE);
        } else {
            mRightLayout.setVisibility(View.GONE);
        }
    }

    /**
     * 设置标题栏不可见
     */
    public void setTitleBarGone() {
        mTitleBarLayout.setVisibility(View.GONE);
    }
    /**
     * 设置标题栏显示
     */
    public void setTitleBarShow() {
        mTitleBarLayout.setVisibility(View.VISIBLE);
    }
    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.actionbar_left_layout) {
            mTitleBarListener.onLeftLayoutListener();
        } else if (id == R.id.actionbar_right_layout) {
            mTitleBarListener.onRightLayoutListener();
        }
    }



}
